package cn.sdormitory.controller;


import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.exam.vo.ExamResultInfoAppVo;
import cn.sdormitory.homeschool.service.HomeSchoolService;
import cn.sdormitory.smartclass.entity.ClassAttence;
import cn.sdormitory.smartclass.entity.ClassDv;
import cn.sdormitory.smartclass.vo.ClassAttenceVo;
import cn.sdormitory.smartclass.vo.ClassDvAppVo;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.entity.StudentTrajectory;
import cn.sdormitory.smartdor.vo.SdAttenceAppVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/app/home/school")
public class AppHomeSchoolController {

    @Autowired
    HomeSchoolService homeSchoolService;

    @GetMapping("/noLogin")
    public CommonResult<String> noLogin(String token) throws ApiException {
        String phone = homeSchoolService.noLogin(token);
        if(phone == null){
            return CommonResult.failed();
        }else{
            return CommonResult.success(phone);
        }
    }

    @PostMapping("/parentLogin")
    public CommonResult<String> parentLogin(@RequestBody Map<String,String> loginParam) throws ApiException {
        String token = homeSchoolService.parentLogin(loginParam);
        if(token != null){
            return CommonResult.success(token);
        }
        return CommonResult.failed();
    }

    @GetMapping("/getVerifyCode/{phone}")
    public CommonResult<String> getVerifyCode(@PathVariable("phone")String phone) throws ApiException {
        homeSchoolService.sendValidCode(phone);
        return CommonResult.success("发送成功");
    }

    @GetMapping("/getParenChildExamResult/{phone}")
    public CommonResult getParenChildExamResult(@PathVariable("phone")String phone){
        List<ExamResultInfoAppVo> list = homeSchoolService.getParenChildExamResult(phone);
        if(list == null || list.size() <= 0){
            return CommonResult.failed("该学生暂无考试成绩!");
        }else {
            return CommonResult.success(list);
        }
    }

    @GetMapping("/getParenChildDv/{phone}")
    public CommonResult getParenChildDv(@PathVariable("phone")String phone){
        List<ClassDvAppVo> list = homeSchoolService.getParenChildDv(phone);
        if(list == null || list.size() <= 0){
            return CommonResult.failed("该学生无违纪信息!");
        }else {
            return CommonResult.success(list);
        }
    }

    @GetMapping("/getParenChildClassAttendance/{phone}")
    public CommonResult getParenChildClassAttendance(@PathVariable("phone")String phone){
        List<ClassAttenceVo> list = homeSchoolService.getParenChildClassAttendance(phone);
        if(list == null || list.size() <= 0){
            return CommonResult.failed("该学生班级考勤数据无异常!");
        }else {
            return CommonResult.success(list);
        }
    }

    @GetMapping("/getParenChildDorAttendance/{phone}")
    public CommonResult getParenChildDorAttendance(@PathVariable("phone")String phone){
        List<SdAttenceAppVo> list = homeSchoolService.getParenChildDorAttendance(phone);
        if(list == null || list.size() <= 0){
            return CommonResult.failed("该学生宿舍考勤数据无异常!");
        }else {
            return CommonResult.success(list);
        }
    }

    @GetMapping("/getParenChildTrajectory/{phone}")
    public CommonResult getParenChildTrajectory(@PathVariable("phone")String phone){
        List<StudentTrajectory> list = homeSchoolService.getParenChildTrajectory(phone);
        if(list == null || list.size() <= 0){
            return CommonResult.failed("该学生无进出校园记录!");
        }else {
            return CommonResult.success(list);
        }
    }
}
